package com.sp.file;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

/**
 * 演示创建文件
 */
public class FileCreate {
    public static void main(String[] args) {

    }

    //方式1: newFile(String pathname) //根据路径构建一个File对象
    @Test
    public void create01() {
        String filePath = "d:\\development\\code\\IO_Test\\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式2: new File(File parent, String child)//根据父目录文件 + 子路径构建
    @Test
    public void create02() {
        File parentFile = new File("d:\\development\\code\\IO_Test\\");
        String fileName = "news2.txt";
        File file = new File(parentFile, fileName);

        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3: new File(String parent, String child)//根据父目录 + 子路径构建
    @Test
    public void create03() {
        String parentPath = "d:\\development\\code\\IO_Test\\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //其他路径编写方式
    @Test
    public void create04() {
        String parentPath = "d:/development/code/IO_Test/";
        String fileName = "news4.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
